﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using EntityManage;
using BusinessFacade;
using System.IO;

public partial class Modules_Default_MyAppealAdd : System.Web.UI.Page
{
    IOther_AppealHandler appealHandler = new Other_AppealManage();
    IOther_AppealAttachHandler appAttachHandler = new Other_AppealAttachManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!CheckPurviews())
        {
            Response.Redirect("~/Login.aspx");
        }
        if (!IsPostBack)
        {
            //int businessId = int.Parse(Request["bid"]);
            //string where = " Business_ID = @Business_ID";
            //SqlParameter[] parms = new SqlParameter[] { new SqlParameter("@Business_ID", businessId) };
            //int counter = appealHandler.GetCounter(where, parms);
            //if (counter > 0)
            //{
            //    Jscript.Alert("该记录已进行过申诉", this.Page);
            //    Jscript.ReturnAndClose(this.Page, "true");
            //}
        }
    }

    private bool CheckPurviews()
    {
        if (Session["UserPurviews"] != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        int businessId = int.Parse(Request["bid"]);
        int userId = int.Parse(Session["CurrentUserID"].ToString());

        HttpFileCollection files = HttpContext.Current.Request.Files;

        Other_AppealQuery appealQuery = new Other_AppealQuery();
        appealQuery.User_ID = userId;
        appealQuery.Title = txtTitle.Text;
        appealQuery.Content = txtContent.Text;
        appealQuery.Status = (int)Enums.OtherAppealStatus.NewAdded;
        appealQuery.Business_ID = businessId;
        appealQuery.Create_Time = DateTime.Now;

        int appealId = 0;
        appealHandler.InsertOther_Appeal(appealQuery, ref appealId);

        // 添加附件
        if (appealId != 0 && files != null && files.Count > 0)
        {
            for (int i = 0; i < files.Count; i++)
            {
                HttpPostedFile postedFile = files[i];
                string fileName, fileExtension;
                fileName = System.IO.Path.GetFileName(postedFile.FileName);
                if (fileName != "")
                {
                    fileExtension = System.IO.Path.GetExtension(fileName);
                    string path = System.Web.HttpContext.Current.Request.MapPath(Enums.Other_Appeal_Attach_Path + appealId.ToString());
                    if (!Directory.Exists(path))
                        Directory.CreateDirectory(path);
                    postedFile.SaveAs(path + "/" + fileName);
                }

                Other_AppealAttachQuery attchQuery = new Other_AppealAttachQuery();
                attchQuery.Appeal_ID = appealId;
                attchQuery.Name = fileName;
                attchQuery.Size = postedFile.ContentLength;
                attchQuery.Path = Enums.Other_Appeal_Attach_Path + appealId.ToString() + "/" + fileName;
                appAttachHandler.InsertOther_AppealAttach(attchQuery);
            }
        }

        Jscript.Alert("添加成功");
        Jscript.ReturnAndClose(this.Page, "true");
    }

}
